<!DOCTYPE html>
<html>
<head>
    <title>系统配置管理</title>
    <#include "/common/import.ftl" >
    <script type="text/javascript" src="${ctx}/js/utiltools.js"></script>
    <script type="text/javascript">
        $(function () {
            $('#timeTaskList').datagrid({
                title: '系统配置列表',
                url: '${ctx }/config/list!data.action',
                striped: true,
                collapsible: true,
                pagination: true,
                singleSelect: true,
                nowrap: true,//数据长度超出列宽时将会自动截取。
                rownumbers: true,//行号
                fitColumns: true,//自动使列适应表格宽度以防止出现水平滚动
                remoteSort: false,
                idField: 'fileName',
                columns: [[
                    {
                        field: 'fileName',
                        title: '配置文件名称',
                        width: 200,
                        align: 'center',
                        sortable: true,
                        formatter: function (value) {
                            var val = "";
                            if (value == "unity_config.properties") {
                                val = "统一配置文件";
                            } else if (value == "uct_client.properties") {
                                val = "UCT客户端配置文件";
                            } else if (value == "custom-method.properties") {
                                val = "实体对象IUS配置文件";
                            } else if (value == "white.xml") {
                                val = "默认白名单配置文件";
                            } else if (value == "uct_white.xml") {
                                val = "UCT白名单配置文件";
                            } else if (value == "sso_white.xml") {
                                val = "SSO白名单配置文件";
                            } else if (value == "portal_white.xml") {
                                val = "portal白名单配置文件";
                            } else if (value == "flow_white.xml") {
                                val = "flow白名单配置文件";
                            } else if (value == "ius_white.xml") {
                                val = "ius白名单配置文件";
                            } else if (value == "bid_white.xml") {
                                val = "bid竞价白名单配置文件";
                            }
                            return val + "(" + value + ")";
                        }
                    },
                    {
                        field: 'updateTime',
                        title: '更新时间',
                        width: 160,
                        align: 'center',
                        sortable: true,
                        formatter: function (value) {
                            return getFormatDate(new Date(value));
                        }
                    }
                ]],
                toolbar: [{
                    text: '增加',
                    iconCls: 'icon-add',
                    handler: function () {
                        saveCate();
                    }
                }, '-', {
                    text: '删除',
                    iconCls: 'icon-remove',
                    handler: function () {
                        deleteCate();
                    }
                }, '-', {
                    text: '修改',
                    iconCls: 'icon-edit',
                    handler: function () {
                        editCate();
                    }
                }, '-', {
                    text: '刷新缓存',
                    iconCls: 'icon-search',
                    handler: function () {
                        loadCache();
                    }
                }, '-', {
                    text: '查看缓存',
                    iconCls: 'icon-edit',
                    handler: function () {
                        showRestartDialog();
                    }
                }
                ]
            });
        });

        function loadCache() {
            $.ajax({
                type: "get",
                url: "${ctx}/config/loadCache",
                dataType: "json",
                success: function (data) {
                    var result = data;//jQuery.parseJSON(data);
                    if (result.flag == 'T') {
                        $.messager.alert('结果', '操作成功', 'info');
                    } else if (result.flag == 'F') {
                        $.messager.alert('结果', result.msg, 'info');
                    } else {
                        $.messager.alert('结果', '操作失败，请重试', 'error');
                    }
                },
                error: function (messg) {
                    $.messager.alert('错误提示', '操作失败:' + messg.responseText, 'error');
                }
            });
        }

        // 进入增加页面
        function saveCate() {
            $('#saveFrame').html('');
            var url = '${ctx}/config/save';
            $('#saveFrame').attr("title", '');
            $('#saveFrame').attr("src", url);
            $('#saveDiv').window('open');
        }

        // 点击取消按钮，关闭添加窗口
        function colseAdd() {
            $('#saveDiv').window('close');
        }

        // 刷新列表
        function cateReload() {
            $('#timeTaskList').datagrid('reload');
        }

        // 判断是否选中一条记录
        function getSelected() {
            var selected = $('#timeTaskList').datagrid('getSelected');
            if (selected) {
                return selected;
            } else {
                $.messager.alert('提示', '请选择要操作的数据', 'info');
            }
        }

        // 进入修改页面
        function editCate() {
            var node = getSelected();
            if (node) {
                var url = '${ctx}/config/save?id=' + node.fileName;
                $('#saveFrame').attr("title", node.fileName);
                $('#saveFrame').attr("src", url);
                $('#saveDiv').window('open');
            }
        }

        //删除，物理删除
        function deleteCate() {
            var node = getSelected();
            if (node) {
                $.messager.confirm('确认', '您确定要删除:<font color=red>' + node.fileName + '</font> ？', function (r) {
                    if (r) {
                        $.ajax({
                            type: "post",
                            url: "${ctx}/config/del.action?id=" + node.fileName,
                            dataType: "json",
                            success: function (data) {
                                var result = data;//jQuery.parseJSON(data);
                                if (result.flag == 'T') {
                                    $.messager.alert('结果', '操作成功', 'info');
                                    var rowIndex = $('#timeTaskList').datagrid('getRowIndex', node);
                                    $('#timeTaskList').datagrid('deleteRow', rowIndex);
                                } else if (result.flag == 'F') {
                                    $.messager.alert('结果', result.msg, 'info');
                                } else {
                                    $.messager.alert('结果', '操作失败，请重试', 'error');
                                }
                            },
                            error: function (messg) {
                                $.messager.alert('错误提示', '操作失败:' + messg.responseText, 'error');
                            }
                        });
                    }
                });
            }
        }

    </script>
</head>
<body>
<table id="timeTaskList"></table>

<!-- 添加窗口 -->
<div id="saveDiv" class="easyui-window" title="系统配置"
     style="padding:5px;width: 850px; height:450px; overflow: hidden;"
     iconCls="icon-search" closed="true" maximizable="false" minimizable="false"
     collapsible="false">
    <iframe frameborder="0" id="saveFrame" height="100%" width="99%" scrolling="yes"
            frameborder="0"></iframe>
</div>
<div id="restartDialog" class="easyui-dialog" title="查看缓存信息" style="width: 800px; height: 300px;"
     data-options="iconCls:'pag-list',modal:true,collapsible:false,minimizable:false,maximizable:false,resizable:false,closed:true">
    <textarea id="viewCache" cols="120" rows="15"></textarea>
</div>
<script>
    function showRestartDialog() {
        var node = getSelected();
        if (node) {
            var url = "${ctx}/viewCache?type=config&id=" + node.fileName;
            $.ajax({
                type: "get",
                url: url,
                dataType: "text",
                success: function (data) {
                    $("#viewCache").html(data);
                    $("#restartDialog").dialog('open');
                },
                error: function (messg) {
                    $.messager.alert('错误提示', '操作失败:' + messg.responseText, 'error');
                }
            });
        }
    }

    /**
     *转换日期对象为日期字符串
     * @param l long值
     * @param pattern 格式字符串,例如：yyyy-MM-dd hh:mm:ss
     * @return 符合要求的日期字符串
     */
    function getFormatDate(date, pattern) {
        if (date == undefined) {
            date = new Date();
        }
        if (pattern == undefined) {
            pattern = "yyyy-MM-dd hh:mm:ss";
        }
        return date.format(pattern);
    }

    //扩展Date的format方法
    Date.prototype.format = function (format) {
        var o = {
            "M+": this.getMonth() + 1,
            "d+": this.getDate(),
            "h+": this.getHours(),
            "m+": this.getMinutes(),
            "s+": this.getSeconds(),
            "q+": Math.floor((this.getMonth() + 3) / 3),
            "S": this.getMilliseconds()
        }
        if (/(y+)/.test(format)) {
            format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
            }
        }
        return format;
    }
</script>
</body>
</html>